from dotenv import dotenv_values
import redis
import sys

config = dotenv_values('.flaskenv')  # 加载 .flaskenv 文件中定义的环境变量
REDIS_HOST = config.get('REDIS_HOST')
REDIS_PORT = config.get('REDIS_PORT')
REDIS_PASSWORD = config.get("REDIS_PASSWORD") or ""
# REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/0"
# print(f"redis_url = {REDIS_URL}")
# REDIS_URL = f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/0"
SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=0)  # Redis服务器配置


def check_redis():
    try:
        SESSION_REDIS.ping()
        print("redis连接成功")
    except Exception as e:
        print(REDIS_HOST, REDIS_PORT)
        print(f"Failed to connect to redis: {e}")
        sys.exit(1)
